var beiMiCommon = require("BeiMiCommon");

var ANDROID_CLASS_NAME = "com/shuxing/chess/wxapi/WXEntryActivity"
var JAVA_STRING        = "Ljava/lang/String;"

cc.Class({
    extends: beiMiCommon,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
      manage:{
          default:null,
          type:cc.Prefab
      },
      chat:{
        default:null,
        type:cc.Prefab
      },
      joinClub:{
        default:null,
        type:cc.Prefab
      },
      createClub:{
        default:null,
        type:cc.Prefab
      },
      memberList:{
        default:null,
        type:cc.Prefab
      },
      memberList:{
        default:null,
        type:cc.Prefab
      },
      chatUse:{
        default:null,
        type:cc.Prefab
      },
      mesBox:{
        default:null,
        type:cc.Node
      },
        scrollBox:{
            default:null,
            type:cc.ScrollView
        }
    },

    // use this for initialization
    onLoad: function () {
        // cc.beimi.socket.on("updateclubname" , function(result){
        //     console.log(12345678)
        //     console.log(result)
        // });
    },
    onClick:function(event){
        event.stopPropagation();
    },
    onCloseAll: function(){
        this.closeAll();
        var node = cc.find("Canvas/index");
        node.destroy();
        node.active = false;
    },
    onCloseClick:function(){
        this.closeOpenWin();
    },
    onCloseClick2:function(){
        this.closeOpenWin2();
    },
    onCloseClick3:function(){
        this.closeOpenWin3();
    },
    onCloseClick4:function(){
        this.closeOpenWin4();
    },
    exitGame: function(){
        cc.director.loadScene("login");
        //cc.director.end();
    },
    //刷新俱乐部列表
    refreshClub:function(){
        let self = this;
        var param = "";
        let socket = this.socket();
        socket.emit("getclubs", param);
        var thisEvent = new cc.Event.EventCustom("getclubs", true);
        self.node.dispatchEvent(thisEvent);
    },
    //创建俱乐部
    creatClub:function(){
        let self = this;
        let socket = this.socket();
        var node = cc.find("Canvas/createClub");
        var clubname = node.getChildByName("base1").getChildByName("input").getChildByName("EditBox");
        var param = clubname.getComponent(cc.EditBox).string;
        if(param.length <2){
            self.alert("请输入正确的俱乐部名称！");
            return false
        }
        socket.emit("createclub" , param);
        cc.beimi.socket.on("createclub" , function(result){
            var res = JSON.parse(result)
            if(res.sxGameClub.showid){
                self.closeOpenWin2();
                socket.emit("getclubs", "");
                var thisEvent = new cc.Event.EventCustom("getclubs", true);
                self.node.dispatchEvent(thisEvent);
            }
        });
    },
    //加入俱乐部
    joinInClub:function(){
        var node = cc.find("Canvas/joinClub");
        var clubname = node.getChildByName("base1").getChildByName("input").getChildByName("EditBox");
        var clubid = clubname.getComponent(cc.EditBox).string;
        let self = this;
        var param = {
            clubid: clubid
        };
        //console.log(param)
        let socket = this.socket();
        socket.emit("joinclub" , param);
        cc.beimi.socket.on("joinclub" , function(result){
            var res = JSON.parse(result);
            console.log(res);
            if(res.success){
                self.closeOpenWin2();
                self.refreshClub();
            }
            else{
                self.alert("俱乐部不存在");
                self.closeOpenWin2();
            }
        });

    },
    //解散俱乐部
    closeClub:function(){
        let self = this;
        var param = cc.beimi.clubId;
        let socket = this.socket();
        socket.emit("disbandclub" , param);
        cc.beimi.socket.on("disbandclub" , function(result){
            var res = JSON.parse(result);
            console.log(res);
            self.refreshClub();
        });
    },
    //修改名字
    updateClub:function(){
        var self=this;
        var node = cc.find("Canvas/updateName");
        var newName = node.getChildByName("base1").getChildByName("input").getChildByName("EditBox").getComponent(cc.EditBox).string;
        var param = {
            id: cc.beimi.clubId,
            clubname: newName
        };

        let socket = this.socket();
        socket.emit("updateclubname" , param);
        cc.beimi.socket.on("updateclubname" , function(result){
            var res = JSON.parse(result);
            console.log(res)
            self.onCloseClick3();
            self.onCloseClick2();
            self.refreshClub();
        });
        
    },
    //添加成员
    addMember:function(){
        let self = this;
        var node = cc.find("Canvas/addMember");
        var memberBox = node.getChildByName("base1").getChildByName("input").getChildByName("EditBox");
        var memberId = memberBox.getComponent(cc.EditBox).string;
        var newParam = {
            userid: memberId,
            clubid: cc.beimi.clubId
        };

        let socket = this.socket();
        console.log(newParam)
        socket.emit("addclubmember" , newParam);
        cc.beimi.socket.on("addclubmember" , function(result){
            var res = JSON.parse(result);
            console.log(res)
            self.refreshClub();
            self.alert("该玩家不存在。");
        });
    },
    //移除成员
    removeMember:function(e){
        var showid = e.target.parent.getChildByName("id").getComponent(cc.Label).string;
        let self = this;
        console.log(showid)
        var newParam = {
            userid: showid,
            clubid: cc.beimi.clubId
        };
        let socket = this.socket();
        socket.emit("addclubmember" , newParam);
        cc.beimi.socket.on("addclubmember" , function(result){
            var res = JSON.parse(result);
            console.log(res)
            self.refreshClub();
        });
    },
    onManageClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.manage) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    onChatClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.chat) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    onJoinClubClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.joinClub) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    onCreateClubClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.createClub) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    playTabClick:function(e){
        if(this.playTab!=e.currentTarget.name){
           this.playTab=e.currentTarget.name;
           this.initTab(this.play.children,this.playTab);
        }
    },
    onMemberListClick:function(){
        cc.beimi.openwin2 = cc.instantiate(this.memberList) ;
        cc.beimi.openwin2.parent = cc.find('Canvas');
    },
    sendMes:function(){
        var node = cc.find("Canvas/chat");
        var mesNode = node.getChildByName("base1").getChildByName("message").getChildByName("input").getComponent(cc.EditBox);
        //console.log(message);
        if(mesNode.string){
            //克隆一个预制资源节点
            let group = cc.instantiate(this.chatUse);
            let mesGroup = group.getComponent("chat");
            var obj = {
                logo: null,
                name: null,
                message: mesNode.string
            };
            mesGroup.init(obj);
            //添加到group数组
            //this.group.push(mesGroup);
            //挂载到节点
            group.parent = this.mesBox;
            this.scrollBox.scrollToBottom(0);
            mesNode.string = '';
        }
    },
    //微信分享
    share:function(){
        if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "shareWX","(Z)V",true);
        }
    },
    //闲聊分享
    sgShare:function(){
        if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sgShareLink","(Z)V",true);
        }
    },
    // 分享到好友聊天
    // @share_link 分享链接
    // @title 标题
    // @desc 分享内容
    // shareToFriend:function(share_link, title, desc){
    //     if(cc.sys.os == cc.sys.OS_WINDOWS){
    //         var _type = 1 // 1:聊天分享  2:朋友圈分享
    //         var _methond_name = "ShareLinkToWeChat"
    //         var _methond_singnature = "(" + JAVA_STRING + JAVA_STRING + JAVA_STRING  +"I)Z"
    //         jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_link, title, desc, _type)
    //     }
    // },
    // // 微信文字内容分享到好友
    // // @text 分享的文字内容
    // shareTextToFriend:function(text){
    //     if(cc.sys.os == cc.sys.OS_WINDOWS){
    //         var _type = 1
    //         var _methond_name = "ShareToWeChat"
    //         var _methond_singnature = "("+ JAVA_STRING +"I)Z"
    //         jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, text, _type)
    //     }
    // },
    // 微信截图分享
    // @share_path 截图的路径
    shareImageToWeChat:function(share_path){
        var node = cc.find("Canvas/share");
        var sanm = node.getChildByName("base1").getChildByName("label").getComponent(cc.Label);
        sanm.string = "5";
        let self = this;
        var _methond_name = "ShareImageToWeChat"
        var _methond_singnature = "("+ JAVA_STRING +")Z"
        var _bShareResult = jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_path)
        if(_bShareResult){
            self.alert("分享成功");
        } else {
            self.alert("分享失败");
        }
    },
    // 截图分享
    shareCaptureScreen: function () {

        var size = cc.director.getWinSize();
        var fileName = "result_share.jpg";
        var fullPath = jsb.fileUtils.getWritablePath() + fileName; //拿到可写路径，将图片保存在本地，可以在ios端或者java端读取该文件
        if (jsb.fileUtils.isFileExist(fullPath)) {
            jsb.fileUtils.removeFile(fullPath);
        }
        var texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height));
        texture.setPosition(cc.p(size.width / 2, size.height / 2));
        texture.begin();
        cc.director.getRunningScene().visit(); //这里可以设置要截图的节点，设置后只会截取指定节点和其子节点
        texture.end();
        texture.saveToFile(fileName, cc.IMAGE_FORMAT_JPG);
        this.ShareImageToWeChat(fullPath)

        // 注意，EditBox，VideoPlayer，Webview 等控件无法被包含在截图里面
        // 因为这是 OpenGL 的渲染到纹理的功能，上面提到的控件不是由引擎绘制的
        // var node = cc.find("Canvas/share");
        // var sanm = node.getChildByName("base1").getChildByName("label").getComponent(cc.Label);
        // sanm.string = "1";
        // if(CC_JSB) {
        //     sanm.string = "2";
        //     var renderTexture = cc.RenderTexture.create(1280,720);
        //     var self = this
        //     // 实际截屏的代码
        //     renderTexture.begin();
        //     cc.director.getScene()._sgNode.visit();
        //     renderTexture.end();
        //     sanm.string = "3";
        //     renderTexture.saveToFile("wechat_share_image_screenshot.png",cc.ImageFormat.PNG, true, function () {
        //         // 调用微信sdk接口
        //         var _share_path = jsb.fileUtils.getWritablePath()
        //         sanm.string = "4";
        //         self.ShareImageToWeChat(_share_path)
        //     });
        // }
    }
});
